/*
 *
 *  Copyright (C) 2010 Tal Eisenberg
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package tal.drivechain.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JPanel;
import tal.drivechain.module.properties.ColorParser;

/**
 *
 * @author eisental
 */
public class WindowResizer extends JPanel implements MouseListener, MouseMotionListener {
    private boolean resizing = false;
    private GUIWindow frame;
    private Color color;
    private Color baseColor;
    private Color darker;
    private Point dragDistancePoint = new Point();

    public WindowResizer(GUIWindow frame, Color color) {
        super();
        this.frame = frame;
        Dimension size = new Dimension(8,8);
        this.baseColor = color;
        this.setSize(size);
        this.setMaximumSize(size);
        this.setPreferredSize(size);
        addMouseListener(this);
        addMouseMotionListener(this);
    }

    @Override
    public void addNotify() {
        super.addNotify();
        setColor(baseColor);
    }
    
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (resizing)
            g.setColor(color);
        else g.setColor(darker);
        g.fillRect(0, 0, 8, 8);
    }

    public void mouseClicked(MouseEvent e) { }

    public void mousePressed(MouseEvent e) {
        dragDistancePoint.x = frame.getWidth() - (this.getLocationOnScreen().x - frame.getLocation().x + e.getX());
        dragDistancePoint.y = frame.getHeight() - (this.getLocationOnScreen().y - frame.getLocation().y + e.getY());
        resizing = true;
        repaint();
    }

    public void mouseReleased(MouseEvent e) { 
        resizing = false;
        repaint();
    }

    public void mouseEntered(MouseEvent e) { }

    public void mouseExited(MouseEvent e) { }

    public void mouseDragged(MouseEvent e) {
        Point mouseLoc = MouseInfo.getPointerInfo().getLocation();
        Point frameLoc = frame.getLocation();
        if(resizing){
          Point pt = e.getPoint();
          int newWidth = mouseLoc.x - frameLoc.x + dragDistancePoint.x;
          int newHeight = mouseLoc.y - frameLoc.y + dragDistancePoint.y;
          Dimension min = frame.getMinimumSize();
          if (min.width>newWidth)
              newWidth = min.width;
          if (min.height>newHeight)
              newHeight = min.height;
          frame.setSize(newWidth, newHeight);
          frame.validate();
          repaint();
        }
    }

    public void mouseMoved(MouseEvent me) { }

    void setColor(Color color) {
        if (color==null) return;
        this.baseColor = color;
        this.color = ColorParser.xor(color);
        this.darker = this.color.darker();
        if (this.getParent()!=null) this.getParent().setBackground(baseColor.darker().darker());
        repaint();
    }

}
